<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = path + "/resource/extjs/skins/default";
%>
	<%@ include file="/taglibs.jsp"%>
<html>
	<head>
		<jsp:include page="../common.jsp" />
		<!-- include common.jsp -->
		<title>后台管理</title>
		<base />
		<script type="text/javascript">
</script>
		<script type="text/javascript">
		var uName='<%=request.getParameter("uName")%>';
	var basePath='<%=basePath%>';
	var path='<%=path%>';
	Ext.BLANK_IMAGE_URL =path+"/resource/images/s.gif";
	var form_create_class;
	var win_create_class;
	var from_create_company;
	var win_create_company;
	function addClass(){//添加分类，一级分类和二级分类
		var tree=Ext.getCmp("classTree");
    	var form=form_create_class.getForm();
    	var selectModel=tree.getSelectionModel();
    	var classId="";
    	if(selectModel.selNode!=null){//选中了一种分类 如果选中一级则创建相应的二级分类，如果选中了二级分类则创建公司，不选中则创建一级分类
    		classId=selectModel.selNode.id;
    		if(classId=='mainClass')
    			classId="";
    	}
   		form.submit({
   			url:path+'/classify/createFirstClassify.do',
   			params:{"classId":classId},
   			waitMsg:'load...',
   			success:function(form,action){
   				tree.root.reload();
   				win_create_class.hide();
   				form_create_class.getForm().reset();
   				
   			}	
   		})		
   	};			
    function createCompany(){	
    	var tree=Ext.getCmp("classTree");
    	var form=from_create_company.getForm();
    	var selectModel=tree.getSelectionModel();
    	var classId=selectModel.selNode.id;
    	form.submit({
    		url:path+'/classify/createCompany.do',
    		params:{'classId':classId},
    		waitMsg:'load...',	
    		success:function(form,action){
    			tree.root.reload();
    			win_create_company.hide();
    			from_create_company.getForm().reset();
    		}
    	})
    };
    form_create_class = new Ext.form.FormPanel({
    	frame:true,
    	border : false,
    	items : [{
    		layout:'form',
    		id:'addClassForm',
    		//frame:true,
    		border:false,
    		width:450,
    		height:160,
    		defaultType: 'textfield',
    		bodyStyle:'padding:10px 5px 0 20px',
    		defaults: {width: 230},
    		items: [{
    			fieldLabel: '分类名称',
    			name: 'className',
    			allowBlank:false
    		},{	
    			fieldLabel: '描述',
    			name: 'classDesc'
    		},{
    			fieldLabel: '顺序',
    			name: 'displayOrder'
    		}]	
    	}]		
    });	
    win_create_class=new Ext.Window({
    	title:'分类添加',
    	width:400,
    	height:200,
    	modal:true,
    	items:[form_create_class],
    	closeAction:'hide',
    	plain:true,
    	frame:true,
   		buttons:[{
   			text:'提交',
   			handler:addClass
   		},{
   			text:'关闭',
   			handler:function(){
   				win_create_class.hide();
   			}
   		}]	
   	});		
    
    from_create_company = new Ext.form.FormPanel({		
    	frame:true,
    	border : false,
    	items : [{
	    	layout:'form',
	    	id:'from_add_company',
	    	fileUpload:true,
	    	width:450,
	    	defaultType: 'textfield',
	    	bodyStyle:'padding:10px 5px 0 20px',
	    	defaults: {width: 230},
	    	items: [{
	    		fieldLabel: '公司名称',	
	    		name: 'companyName',	
	    		allowBlank:false	
	    	},{		
	    		fieldLabel: '描述',	
	    		 name: 'companyDesc'	
	    	},{
	    		fieldLabel: '联系人',
	    		name: 'linkMan'
	    	},{
	    		fieldLabel: '电话',
	    		name: 'phone'
	    	},{
                fieldLabel: '公司地址',
                name: 'address'
	        },{
                fieldLabel: '乘车路线',
                name: 'busLine'
	        },{
	    		fieldLabel: '网址',
	    		name: 'siteUrl'
	    	},{		
	    		xtype:'textarea',
	    		height:140,
	    		fieldLabel: '公司简介',
	    		name: 'briefInfo'
	    	},{	
	    		fieldLabel: '顺序',
	    		name: 'displayOrder'
	    	},{	
	    		inputType:'file',
	    		labelWidth:100,
	    		fieldLabel: '图片',
	    		name: 'advertPic'
	    	}]	
	    }]	
    });	
    win_create_company=new Ext.Window({
    	title:'公司添加',
    	width:400,
    	modal:true,
    	items:[from_create_company],
    	closeAction:'hide',
    	plain:true,
    	frame:true,
    	buttons:[{
    		text:'提交',
    		handler:createCompany
    	},{
    		text:'关闭',
    		handler:function(){
    			win_create_company.hide();
    		}
    	}]	
    });	
    //模块添加
    var form_create_module = new Ext.form.FormPanel({
    	frame:true,
    	border : false,
    	items : [{
    		layout:'form',
    		id:'form_create_module',
    		//frame:true,
    		border:false,
    		width:450,
    		height:160,
    		defaultType: 'textfield',
    		bodyStyle:'padding:10px 5px 0 20px',
    		defaults: {width: 230},
    		items: [{
    			fieldLabel: '模块名称',
    			name: 'areaName',
    			allowBlank:false
    		},{	
    			fieldLabel: '描述',
    			name: 'areaDesc'
    		},{
    			fieldLabel: '顺序',
    			name: 'displayOrder'
    		},{
            	xtype: 'radiogroup',
	            fieldLabel: '激活',
	            name:'state',
	            allowBlank:false,
	            items: [
	                {boxLabel: '是', name: 'state', inputValue: 1},
	                {boxLabel: '否', name: 'state', inputValue: 0}
	            ]
       		}]	
    	}]		
    });	
    var win_create_module;
   	win_create_module=new Ext.Window({
    	title:'模块添加',
    	width:400,
    	modal:true,
    	items:[form_create_module],
    	closeAction:'hide',
    	plain:true,
    	frame:true,
    	buttons:[{
    		text:'提交',
    		handler:createModule
    	},{
    		text:'关闭',
    		handler:function(){
    			win_create_module.hide();
    		}
    	}]	
    });	
    //总模块添加
    function createModule(){
    	var areaId="";
    	var tree=Ext.getCmp("moduleTree");
    	var form=form_create_module.getForm();
   		form.submit({
   			url:path+'/module/createArea.do',
   			params:{"areaId":areaId},
   			waitMsg:'load...',
   			success:function(form,action){
   				tree.root.reload();
   				win_create_module.hide();
   				form_create_module.getForm().reset();
   				
   			}	
   		})		
    }
    var form_create_sub_module = new Ext.form.FormPanel({
    	frame:true,
    	border : false,
    	items : [{
    		layout:'form',
    		id:'form_create_sub_module',
    		//frame:true,
    		border:false,
    		width:450,
    		height:160,
    		defaultType: 'textfield',
    		bodyStyle:'padding:10px 5px 0 20px',
    		defaults: {width: 230},
    		items: [{
    			fieldLabel: '位置模块名称',
    			name: 'areaName',
    			allowBlank:false
    		},{	
    			fieldLabel: '描述',
    			name: 'areaDesc'
    		},{
    			fieldLabel: '顺序',
    			name: 'displayOrder'
    		},{
            	xtype: 'radiogroup',
	            fieldLabel: '激活',
	            name:'state',
	            allowBlank:false,
	            items: [
	                {boxLabel: '是', name: 'state', inputValue: 1},
	                {boxLabel: '否', name: 'state', inputValue: 0}
	            ]
       		},{
            	xtype: 'radiogroup',
	            fieldLabel: '位置',
	            id:'location',
	            name:'location',
	            allowBlank:false,
	            items: [
	            	{boxLabel: '左', name: 'location', inputValue: 1},
	            	{boxLabel: '中', name: 'location', inputValue: 2},
	            	{boxLabel: '右', name: 'location', inputValue: 3}
	            ]
       		}]	
    	}]		
    });	
    var win_create_sub_module;
   	win_create_sub_module=new Ext.Window({
    	title:'模块添加',
    	width:400,
    	modal:true,
    	items:[form_create_sub_module],
    	closeAction:'hide',
    	plain:true,
    	frame:true,
    	buttons:[{
    		text:'提交',
    		handler:createSubModule
    	},{
    		text:'关闭',
    		handler:function(){
    			win_create_sub_module.hide();
    		}
    	}]	
    });	
     //总模块添加
    function createSubModule(){
    	var tree=Ext.getCmp("moduleTree");
    	var form=form_create_sub_module.getForm();
    	var selectModel=tree.getSelectionModel();
    	var mvalues=selectModel.selNode.attributes.mvalues;
    	var areaId=selectModel.selNode.id;
    	var flag=0;
    	for(var i=0;i<mvalues.length;i++){
    		if(mvalues[i]==form.getValues().location){
    			if(mvalues[i]==1){
    				alert("该模块已存在！");
    				return;	
    			};
    			flag++;
    			if(flag==2){
    				alert("该模块已存在！");
    				return;	
    			}
    		}
    	}
  		//按location的值创建子模块
   		form.submit({
   			url:path+'/module/createArea.do',
   			params:{"areaId":areaId},
   			waitMsg:'load...',
   			success:function(form,action){
   				tree.root.reload();
   				win_create_sub_module.hide();
   				form_create_sub_module.getForm().reset();
   				
   			}	
   		})		
    };
    //头部广告
    var form_create_head_ad = new Ext.form.FormPanel({
   		layout:'form',
   		id:'form_create_head_ad',
   		frame:true,
   		border:false,
   		width:450,
   		height:160,
   		defaultType: 'textfield',
   		bodyStyle:'padding:10px 5px 0 20px',
   		defaults: {width: 230},
   		reader:new Ext.data.JsonReader({
			root:'data',
			successProperty:'success'
		},[
				{name: 'headAdvertId',mapping:'headAdvertId'},
		        {name: 'headName',mapping:'headName'},
		        {name: 'displayOrder',mapping:'displayOrder'},
		        {name: 'state',mapping:'state'},
		        {name: 'location',mapping:'location'}
		]),
   		items: [{
   			name: 'headAdvertId',
   			hidden:true
   		},{
   			fieldLabel: '位置模块名称',
   			name: 'headName',
   			allowBlank:false
   		},{
   			fieldLabel: '顺序',
   			name: 'displayOrder'
   		},{
           	xtype: 'radiogroup',
            fieldLabel: '激活',
            name:'state',
            allowBlank:false,
            items: [
                {boxLabel: '是', name: 'state', inputValue: 1},
                {boxLabel: '否', name: 'state', inputValue: 0}
            ]
      		},{
           	xtype: 'radiogroup',
            fieldLabel: '位置',
            id:'location',
            name:'location',
            allowBlank:false,
            items: [
            	{boxLabel: '左', name: 'location', inputValue: 1},
            	{boxLabel: '右', name: 'location', inputValue: 3}
            ]
    	}]	
    });	
    var win_create_head_ad;
   	win_create_head_ad=new Ext.Window({
    	title:'模块添加',
    	width:400,
    	modal:true,
    	items:[form_create_head_ad],
    	closeAction:'hide',
    	plain:true,
    	frame:true,
    	buttons:[{
    		text:'提交',
    		handler:createHeadAdvert
    	},{
    		text:'关闭',
    		handler:function(){
    			win_create_head_ad.hide();
    		}
    	}]	
    });	
     //头部大类模块添加
    function createHeadAdvert(){
    	var tree=Ext.getCmp("pictureTree");
    	var form=form_create_head_ad.getForm();
    	var selectModel=tree.getSelectionModel();
    	//var mvalues=selectModel.selNode.attributes.mvalues;
    	//var areaId=selectModel.selNode.id;
    	/*for(var i=0;i<mvalues.length;i++){
    		if(mvalues[i]==form.getValues().location){
    			alert("该模块已存在！");
    			return; 
    		}
    	}*/
  		//创建头部广告大类
   		form.submit({
   			url:path+'/picture/createHeadAd.do',
   			waitMsg:'load...',
   			success:function(form,action){
   				tree.root.reload();
   				win_create_head_ad.hide();
   				form_create_head_ad.getForm().reset();
   				
   			}	
   		})		
    };
   	Ext.onReady(function(){
   		if(uName=="null"){
			window.location.href="login.jsp";
		}
   		var viewport = new Ext.Viewport({
			layout : 'border',
				renderTo:'document.body',
				items : [{
					region : 'west',
					id : 'agreementPanel_viewport',
					title : '系统数据维护',
					split : true,
					width : 260,
					collapsible : true,
					autoScroll : true,
					margins : '2 0 5 5',
					cmargins : '10 5 5 5',
					layout : 'fit',
					items : [
							new Ext.Panel({
								layout:'accordion',
								border:false,
								height:100,
								items:[
									new Ext.Panel({
									title:'分类管理',
									autoScroll:true,
									border:false,
									items:createTemplateTree(),
									buttons:[{
										text:'添加',
										handler:function(){
											var tree=Ext.getCmp("classTree");
											var selectModel=tree.getSelectionModel();
											if(selectModel.selNode!=null){
												var type=selectModel.selNode.attributes.type;
												if(type=='second'){
													win_create_company.show();
													return;
												}else if(type=='company'){
													alert('不能创建');
													return;
												}
											}
											win_create_class.show();
										}
									},{
										text:'删除',
										handler:function(){
											var tree=Ext.getCmp("classTree");
											var selectModel=tree.getSelectionModel();
											if(selectModel.selNode==null){
												alert("请选择删除项");
												return ;
											}else{
												var r=confirm("确认要删除吗?");
												if(r==true){
													var type=selectModel.selNode.attributes.type;
													var id=selectModel.selNode.id;
													Ext.Ajax.request({
														url:path+"/classify/deleteFirstClassify.do",
														params:{"classId":id,"type":type},
														success:function(){
															tree.root.reload();
															Ext.getCmp("centerPanel").removeAll();
														}
													})
												}
											}
										}
									}]
								}),
									new Ext.Panel({
									title:'模块管理',
									border:false,
									autoScroll:true,
									items:createModuleTree(),
									buttons:[{
										text:'添加',
										handler:function(){
											var tree=Ext.getCmp("moduleTree");
									    	var form=form_create_module.getForm();
									    	var selectModel=tree.getSelectionModel();
									    	if(selectModel.selNode==null){
									    		win_create_module.show();
									    		return;
									    	}else if(selectModel.selNode.id=='mainModule'){
									    		win_create_module.show();
									    		return;
									    	}
									    	var type=selectModel.selNode.attributes.type;
									    	if(type=='area'){
									    		var mvalues=selectModel.selNode.attributes.mvalues;
									    		if(mvalues.length==5){
									    			alert("不能创建");
									    			return;
									    		}
									    		win_create_sub_module.show();
									    	}else {
									    		alert("不能创建！");
									    		return;
									    	}
									    	
										}
									},{
										text:'删除',
										handler:function(){
											var tree=Ext.getCmp("moduleTree");
											var selectModel=tree.getSelectionModel();
											if(selectModel.selNode==null){
												alert("请选择删除项");
												return ;
											}else{
												var r=confirm("确认要删除吗?");
												if(r==true){
													var type=selectModel.selNode.attributes.type;
													var id=selectModel.selNode.id;
													var url="";
													var params="";
													if(type=='area'){
														url=path+"/module/delAreaById.do";
														params={"areaId":id};
													}else if(type=='left'){
														url=path+"/module/delLeftRecById.do";
														params={"leftRecommonId":id};
													}else if(type=='middle'){
														url=path+"/module/delMiddleAreaById.do";
														params={"middleAreaId":id};
													}else if(type=='right'){
														url=path+"/module/delRightAreaById.do";
														params={"rightAreaId":id};
													}
													Ext.Ajax.request({
														url:url,
														params:params,
														success:function(){
															tree.root.reload();
															Ext.getCmp("centerPanel").removeAll();
														}
													});
												}
											}
										}
									}]
								}),
									new Ext.Panel({
									title:'广告管理',
									border:false,
									autoScroll:true,
									items:createPictureTree(),
									buttons:[{
										text:'添加',
										id:'addHeadAdvert',
										handler:function(){
											var tree=Ext.getCmp("pictureTree");
									    	var form=form_create_module.getForm();
									    	var selectModel=tree.getSelectionModel();
									    	if(selectModel.selNode==null){
												return ;
											}
									    	var type=selectModel.selNode.attributes.type;
									    	if(type=='ahead'){
									    		win_create_head_ad.show();
									    		return;
									    	}else {
									    		return;
									    	}
										}
									},{
										text:'删除',
										handler:function(){
											var tree=Ext.getCmp("pictureTree");
									    	var form=form_create_module.getForm();
									    	var selectModel=tree.getSelectionModel();
									    	if(selectModel.selNode==null){
									    		alert("选择删除项");
												return ;
											}
									    	var type=selectModel.selNode.attributes.type;
									    	if(type=='head'){
									    		var rt=window.confirm("确定要删除此项吗？");
									    		if(rt==true){
													var id=selectModel.selNode.id;
													Ext.Ajax.request({
														url:path+'/picture/deleteHeadAdvert.do',
														params:{"id":id},
														success:function(){
															tree.root.reload();
															Ext.getCmp("centerPanel").removeAll();
														}
													})
									    		}
									    		return;
									    	}else {
									    		alert("选择删除项");
									    		return;
									    	}
										}
									},{
										text:'修改',
										handler:function(){
											var tree=Ext.getCmp("pictureTree");
									    	var form=form_create_module.getForm();
									    	var selectModel=tree.getSelectionModel();
									    	if(selectModel.selNode==null){
									    		alert("选择修改项");
												return ;
											}
									    	var type=selectModel.selNode.attributes.type;
									    	if(type=='head'){
									    		var id=selectModel.selNode.id;
									    		form_create_head_ad.getForm().load({
									    			url:path+'/picture/loadHeadAdvert.do',
									    			params:{"id":id}
									    		})
									    		win_create_head_ad.show();
									    		return;
									    	}else {
									    		alert("选择修改项");
									    		return;
									    	}
										}
									}]
								})]
							})
						]
						}, {
							region : 'center',
							margins : '2 5 5 0',
							autoScroll : true,
							collapsible : false,
//							autoScroll : true,
							animate : false,
							layout : 'fit',
							border : true,
							items : [
								new Ext.Panel({
									border:false,
									layout:'fit',
									id:'centerPanel',
									buttonAlign:'right',
									items:[]
								})
							]
						}]
				});
	    })
	   Ext.Ajax.timeout=30000;    
	    //分类的树
	   function createTemplateTree() {
	   	var tree = new Ext.tree.TreePanel({
	   		cls : 'empty',
	   		autoScroll : true,
	   		height : 'auto',
	   		rootVisible : true,
	   		containerScroll : true,
	   		lines : true,// 节点之间连接的横竖线
	   		//useArrows : true,// 小箭头
	   		border : false,
	   		id:'classTree',
	   		root :new Ext.tree.AsyncTreeNode({
	   			id:'mainClass',
	   			text:"分类",
	   			draggable:false,
	   			expanded:true
	   		}),
	   		loader:new Ext.tree.TreeLoader({
	   			dataUrl:path+'/classify/getFirstClassify.do'
	   		})	
	   	});
	   	tree.on('click', function(node) {
	   		var tempPath;
	   		if (node.attributes.type== "first") {
	   			tempPath= path+"/manage/classify/updateFirstClassify.jsp?classId="+node.id;
	   		} else if(node.attributes.type=="second"){
	   			tempPath= path+"/manage/classify/updateSecondClassify.jsp?classId="+node.id;
	   		} else if(node.attributes.type=="company"){
	   			tempPath=path+"/manage/classify/updateCompany.jsp?companyId="+node.id;
	   		} else{
	   			return;
	   		}
	   		Ext.getCmp("centerPanel").removeAll();
	   		var temp_items ={
	   			layout : 'fit',
	   			margins : '0 0 0 0',
	   			border : false,
	   			title:node.text,
	   			id : node.id,
	   			closable : true,
	   			html : '<iframe  frameborder="0" width="100%" height="100%" src="'
	   				+ tempPath + '"></iframe>'
	   			}
	   			Ext.getCmp("centerPanel").add(temp_items);
				Ext.getCmp("centerPanel").doLayout();
	   	});
	   	return tree;
	   }
	   //模块的树		
	   function createModuleTree() {
		var tree = new Ext.tree.TreePanel({
	   		cls : 'empty',
	   		autoScroll : true,
	   		height : 'auto',
	   		rootVisible : true,
	   		containerScroll : true,
	   		lines : true,// 节点之间连接的横竖线
	   		//useArrows : true,// 小箭头
	   		border : false,
	   		id:'moduleTree',
	   		root :new Ext.tree.AsyncTreeNode({
	   			id:'mainModule',
	   			text:"模块",
	   			draggable:false,
	   			expanded:true
	   		}),
	   		loader:new Ext.tree.TreeLoader({
	   			dataUrl:path+'/module/getAllArea.do'
	   		})	
	   	});
	   	tree.on('click', function(node) {
	   		var tempPath;
	   		if (node.attributes.type== "area") {
	   			tempPath= path+"/manage/module/updateArea.jsp?areaId="+node.id;
	   		} else if(node.attributes.type=="left"){
	   			tempPath= path+"/manage/module/updateLeftRecommon.jsp?leftRecommonId="+node.id;
	   		} else if(node.attributes.type=="middle"){
	   			tempPath=path+"/manage/module/updateMiddleArea.jsp?middleAreaId="+node.id;
	   		} else if(node.attributes.type=="right"){
	   			tempPath=path+"/manage/module/updateRightArea.jsp?rightAreaId="+node.id;
	   		} else{
	   			return;
	   		}
	   		Ext.getCmp("centerPanel").removeAll();
	   		var temp_items ={
	   			layout : 'fit',
	   			margins : '0 0 0 0',
	   			border : false,
	   			title:node.text,
	   			id : node.id,
	   			closable : true,
	   			html : '<iframe  frameborder="0" width="100%" height="100%" src="'
	   				+ tempPath + '"></iframe>'
	   			}
	   			Ext.getCmp("centerPanel").add(temp_items);
				Ext.getCmp("centerPanel").doLayout();
	   	});
	   	return tree;
	}
	//广告的树		
	   function createPictureTree() {
		var tree = new Ext.tree.TreePanel({
	   		cls : 'empty',
	   		autoScroll : true,
	   		height : 'auto',
	   		rootVisible : true,
	   		containerScroll : true,
	   		lines : true,// 节点之间连接的横竖线
	   		//useArrows : true,// 小箭头
	   		border : false,
	   		id:'pictureTree',
	   		root :new Ext.tree.AsyncTreeNode({
	   			id:'mainPicture',
	   			text:"图片管理",
	   			draggable:false,
	   			expanded:true
	   		}),
	   		loader:new Ext.tree.TreeLoader({
	   			dataUrl:path+'/picture/getAllPicManage.do?uName='+uName
	   		})	
	   	});
	   	tree.on('click', function(node) {
	   		var tempPath;
	   		if (node.attributes.type== "left") {
	   			tempPath= path+"/manage/picture/leftRecPicList.jsp?leftRecId="+node.id;
	   		} else if(node.attributes.type=="middle"){
	   			tempPath= path+"/manage/picture/middleAreaPicList.jsp?middleAreaId="+node.id;
	   		} else if(node.attributes.type=="right"){
	   			tempPath=path+"/manage/picture/rightAreaPicList.jsp?rightAreaId="+node.id;
	   		} else if(node.attributes.type=="head"){
	   			tempPath=path+"/manage/picture/headAdvertList.jsp?headAdId="+node.id;
	   		} else if(node.attributes.type=="headPic"){
	   			tempPath=path+"/manage/picture/headAdvertPic.jsp";
	   		} else if(node.attributes.type=="secondPic"){
	   			tempPath=path+"/manage/picture/secondPicList.jsp";
	   		}else if(node.attributes.type=="user"){
	   			tempPath=path+"/manage/picture/userList.jsp";
	   		} else{
	   			return;
	   		}
	   		Ext.getCmp("centerPanel").removeAll();
	   		var temp_items ={
	   			layout : 'fit',
	   			margins : '0 0 0 0',
	   			border : false,
	   			title:node.text,
	   			id : node.id,
	   			closable : true,
	   			html : '<iframe  frameborder="0" width="100%" height="100%" src="'
	   				+ tempPath + '"></iframe>'
	   			}
	   			Ext.getCmp("centerPanel").add(temp_items);
				Ext.getCmp("centerPanel").doLayout();
	   	});
	   	return tree;
	}
</script>
	</head>
	<body>
	</body>
</html>
